<?php

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Support\Facades\Log;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use PhpMqtt\Client\MqttClient;

class Process485 implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $str;
    protected $deviceId;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($str, $device_id)
    {
        $this->str = $str;
        $this->deviceId = $device_id;
        //var_dump($this->deviceId);
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle(MqttClient $client)
    {
        $connectionSettings = (new \PhpMqtt\Client\ConnectionSettings)
            ->setUsername(env('MQTT_USERNAME'))
            ->setPassword(env('MQTT_PASSWORD'));
        $client->connect($connectionSettings, true);
        sleep(2);
        $client->publish('/iot/whd/network/r/network_test/1234', hex2bin($this->str), 0);
        $client->disconnect();
        // 今后应该要区分，应该每个网关分别设置一个循环
//        switch ($this->deviceId) {
//            case 337:
//                Log::info('Processing job deal ' . $this->deviceId);
//                break;
//            case 338:
//                Log::info('Processing job deal ' . $this->deviceId);
//                break;
//        }
    }
}
